package com.Action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.project.bean.Contract;
import com.project.bean.*;

public class SalesReadExcel {
     public SalesReadExcel(){}
	 public int SalesReadExcel(String path) {
		// TODO Auto-generated method stub
        int result =0;
		// 引入需要解析的EXCEL文件
		File file = new File(path);
		System.out.println("aaa");
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
			// 获取第一个工作表
			// HSSFSheet sheet = workbook.getSheet("sheet0");
			HSSFSheet sheet = workbook.getSheetAt(0);
			int firstRowNum = 0;
			// 获取sheet中最后一行行号
			int lastRowNum = sheet.getLastRowNum();
			System.out.println(lastRowNum);
			List<Sales> sl = new ArrayList<>();
			for (int i = 2; i <=lastRowNum; i++) {
				HSSFRow row = sheet.getRow(i);
				// 获取当前最后单元格列号
				int lastCellNum = row.getLastCellNum();
				Sales s = new Sales();
				// for(int j=0;j<lastCellNum;++j) {
				// HSSFCell cell =row.getCell(j);
				// String value = cell.getStringCellValue();
				// cell.setCellValue("aaaaa");
				 //System.out.print(value + " ");
				// }
				int j = 0;
				HSSFCell cell = row.getCell(j);
				
				String id = cell.getStringCellValue();
				s.setId(id);
				
				
				cell = row.getCell(++j);
				String chinesename = cell.getStringCellValue();
				s.setChinesename(chinesename);
				cell = row.getCell(++j);
				String englishname = cell.getStringCellValue();
				s.setEnglishname(englishname);
				
				cell = row.getCell(++j);
				String sex = cell.getStringCellValue();
				s.setSex(sex);
				
				cell = row.getCell(++j);
				String birthday = cell.getStringCellValue();
				s.setBirthday(birthday);
				
				cell = row.getCell(++j);
				String reservation = cell.getStringCellValue();
				s.setReservation(reservation);
				
				cell = row.getCell(++j);
				String attend = cell.getStringCellValue();
				s.setAttend(attend);
				
				cell = row.getCell(++j);
				String messagefrom = cell.getStringCellValue();
				s.setMessagefrom(messagefrom);
				
				cell = row.getCell(++j);
				String marketman = cell.getStringCellValue();
				s.setMarketman(marketman);
				
				cell = row.getCell(++j);
				String counselor = cell.getStringCellValue();
				s.setCounselor(counselor);
				
				cell = row.getCell(++j);
				String phonecounselor = cell.getStringCellValue();
				s.setPhonecounselor(phonecounselor);
				
				cell = row.getCell(++j);
				String distributionstate = cell.getStringCellValue();
				s.setDistributionstate(distributionstate);
				
				cell = row.getCell(++j);
				String distributiontime = cell.getStringCellValue();
				s.setDistributiontime(distributiontime);
				
				cell = row.getCell(++j);
				String phone1 = cell.getStringCellValue();
				s.setDistributiontime(phone1);
				
				cell = row.getCell(++j);
				String phone2 = cell.getStringCellValue();
				s.setDistributiontime(phone2);
				
				cell = row.getCell(++j);
				String remarks = cell.getStringCellValue();
				s.setDistributiontime(remarks);
				
				cell = row.getCell(++j);
				String username = cell.getStringCellValue();
				s.setUsername(username);
				sl.add(s);
			}
			System.out.println("aaa");
			SalesInputDB idb=new SalesInputDB();
			result =idb.InputDB(sl);;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
             return result;
	}

}

